﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Linq.Dynamic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WXYW.BLL;
using WXYW.DAL;
using System.Collections;
using WXYW.Model;

namespace WXYW
{
    public partial class ManageSparePart : System.Web.UI.Page
    {
        public int RecordsNumber { get; set; }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindSparePartsList();
            }
        }

        private void BindSparePartsList()
        {
            SparePartService sparePartService = new SparePartService();
            List<SparePartModel> models = sparePartService.GetAllSparePartModel(keyWordTextBox.Text.Trim());
            RecordsNumber = models.Count;
            sparePartsGridView.DataSource = models;
            sparePartsGridView.DataBind();
        }


        protected void saveTransferBtn_Click(object sender, EventArgs e)
        {

        }

        protected void saveExitTransferBtn_Click(object sender, EventArgs e)
        {
            SparePartService sparePartService = new SparePartService();
            sparePartService.TransferSparePart(Convert.ToInt32(sparePartIDHiddenField.Value), Convert.ToInt32(transferDestionDropdownList.SelectedValue),
                transferStatusDropdownList.SelectedValue, Convert.ToInt32(transferCountTxtBox.Text), ContextService.CurrentAccount);

            BindSparePartsList();
            TansferProductPopupExtender.Hide();
        }

        protected void TransferProductPanel_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                tranferManagerLbl.Text = ContextService.CurrentAccount.UserName;

                ServiceStationService serviceStationService = new ServiceStationService();
                List<ServiceStation>  serviceStations= serviceStationService.GetALLServiceStations().ToList();
                ServiceStation saleStation = new ServiceStation();
                saleStation.ID = -1;
                saleStation.Name = "出售";
                ServiceStation garbageStation = new ServiceStation();
                garbageStation.ID = -2;
                garbageStation.Name = "报废";
                serviceStations.Add(saleStation);
                serviceStations.Add(garbageStation);
                transferDestionDropdownList.DataSource = serviceStations;
                transferDestionDropdownList.DataBind();
            }
        }

        protected void sparePartsGridView_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName.Equals("Transfer"))
            {
                SparePartService sparePartService = new SparePartService();
                sparePartIDHiddenField.Value = e.CommandArgument.ToString();
                SparePart sparePartEntity = sparePartService.GetSparePartEntityByID(Convert.ToInt32(e.CommandArgument));
                transferModelNOTxtBox.Text = sparePartEntity.ModelNumber;
                transferVersionNOTxtBox.Text = sparePartEntity.VersionNumber;
                transferProductCountTxtBox.Text = sparePartEntity.Count.ToString();
                TansferProductPopupExtender.Show();
            }

        }

        protected void sparePartsGridView_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.DataItem != null)
            {
                e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#FFFF99'");
                e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");
            }

            if (e.Row.RowType == DataControlRowType.Pager)
            {
                DropDownList pageDDL = e.Row.FindControl("pageDDL") as DropDownList;
                for (int index = 1; index <= sparePartsGridView.PageCount; index++)
                {
                    pageDDL.Items.Add(index.ToString());
                }
                pageDDL.SelectedValue = (sparePartsGridView.PageIndex + 1).ToString();
            }
        }

        protected IEnumerable GridView_MustAddARow(IEnumerable data)
        {
            List<SparePartModel> dds = new List<SparePartModel>();
            dds.Add(new SparePartModel());
            return dds;
        }

        protected void pageDDL_SelectedIndexChanged(object sender, EventArgs e)
        {
            DropDownList pageDDL = sender as DropDownList;
            sparePartsGridView.PageIndex = Convert.ToInt32(pageDDL.SelectedIndex);
            BindSparePartsList();
        }

        protected void sparePartApplyFormsGridView_DataBound(object sender, EventArgs e)
        {
            if (sparePartsGridView.BottomPagerRow != null)
            {
                sparePartsGridView.BottomPagerRow.Visible = true;
            }
        }

        protected void sparePartsGridView_Sorting(object sender, GridViewSortEventArgs e)
        {
            SparePartService sparePartService = new SparePartService();
            IQueryable<SparePartModel> models = sparePartService.GetAllSparePartModel("").AsQueryable();
            RecordsNumber = models.Count();

            string sortDirection = "asc";
            if (Session["OrderDirection"] != null)
            {
                sortDirection = Session["OrderDirection"].ToString();
                if (sortDirection.Equals("asc"))
                {
                    Session["OrderDirection"] = "desc";
                }
                else
                {
                    Session["OrderDirection"] = "asc";
                }
            }
            else
            {
                Session["OrderDirection"] = "asc";
            }

            sparePartsGridView.DataSource = models.OrderBy(string.Format("{0} {1}", e.SortExpression, sortDirection));
            sparePartsGridView.DataBind();
        }

        protected void searchLinkButton_Click(object sender, EventArgs e)
        {
            BindSparePartsList();
        }

        protected void resetLinkButton_Click(object sender, EventArgs e)
        {
            keyWordTextBox.Text = "";
            BindSparePartsList();
        }

        protected void editPriceLnkBtn_Click(object sender, EventArgs e)
        {
            //设置价格默认值
            LinkButton lnkBtn = sender as LinkButton;
            SparePartService sparePartService = new SparePartService();
            SparePart sp = sparePartService.GetSparePartEntityByID(Convert.ToInt32(lnkBtn.CommandArgument));
            if (null != sp)
            {
                editMarketPriceTxtBox.Text = sp.MarketPrice;
                editSparePartPriceTxtBox.Text = sp.SparePartPrice;
                IDHiddenField.Value = sp.ID.ToString();
            }        

            EditPriceModal.Show();
        }

        protected void savePriceBtn_Click(object sender, EventArgs e)
        {
            try
            {
                SparePartService sparePartService = new SparePartService();
                SparePart sp = sparePartService.GetSparePartEntityByID(Convert.ToInt32(IDHiddenField.Value));
                sp.MarketPrice = editMarketPriceTxtBox.Text;
                sp.SparePartPrice = editSparePartPriceTxtBox.Text;

                sparePartService.UpdateSparePart(sp);
                EditPriceModal.Hide();
                BindSparePartsList();
            }
            catch (Exception ex)
            {
                EditMsgLbl.Text = string.Format("系统错误:{0}", ex.Message);
            }
        }

        protected void sparePartsGridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            sparePartsGridView.PageIndex = e.NewPageIndex;
        }
    }
}
